﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SW_SoftBully.Controllers;
using SW_SoftBully.Models.ModuloAdministracion.DistritoPeru;
using SW_SoftBully.Models.ModuloAdministracion.ProvinciaPeru;
using System.Data.SqlClient;
using log4net;

namespace SW_SoftBully.Models.ModuloAdministracion.DistritoPeru
{
    public class DistritoPeruDAO
    {
        public List<DistritoPeruBean> ListarDistritoPeru(ProvinciaPeruBean provincia)
        {
            try
            {
                List<DistritoPeruBean> listaDistritoPeru = new List<DistritoPeruBean>();

                String cadenaConfiguracion = ConfigurationManager.ConnectionStrings["SoftBully"].ConnectionString;

                SqlConnection sqlCon = new SqlConnection(cadenaConfiguracion);

                sqlCon.Open();

                string commandString = "SELECT * FROM Distrito " +
                                       "WHERE Id_Provincia = " + provincia.Id_Provincia;

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read())
                {
                    DistritoPeruBean DistritoPeru = new DistritoPeruBean();
                    DistritoPeru.Nombre = Convert.ToString(dataReader["Nombre"]);
                    DistritoPeru.Id_Distrito = Convert.ToInt32(dataReader["Id_Distrito"]);
                    DistritoPeru.Id_Provincia = Convert.ToInt32(dataReader["Id_Provincia"]);
                    listaDistritoPeru.Add(DistritoPeru);
                }
                dataReader.Close();
                sqlCon.Close();

                return listaDistritoPeru;
            }
            catch (Exception e)
            {
                String men = e.ToString();
                provincia.conexion = men;
                return null;
            }
        }
    }
}